/*
 * vmpaste.c
 */

#include <stdio.h>
#include "vmbdoor.h"

/*
 * main
 */
int main ()
{
	uint32 length, command, pref;
	unsigned char text[4];
	int i;
	
	/* are we running in VMware ? 	*/
	if (VMGetVersion() == 0) {
		fprintf(stderr, "Not running inside VMware.\n");
		return 0;
	}

	/* is copy & paste enabled ?	*/
	command = VMCMD_GET_SETTING;
	VMBackDoor(&pref, NULL, &command, NULL);
	
	if (!(pref & VMPREF_COPY_PASTE)) {
		fprintf(stderr, "Copy & paste is disabled.\n");
		return 0;
	}

	/* get clipboard data length	*/
	length = 0;
	command = VMCMD_GET_CLIPLEN;
	
	VMBackDoor(&length, NULL, &command, NULL);
	
	if (length) {
		for (;;) {
			command = VMCMD_GET_CLIPDATA;
			
			VMBackDoor((uint32 *)text, NULL, &command, NULL);
			
			if (length < 4) {
				text[length] = '\0';
			}

			/* returned data length is likely to be slightly larger than	*/
			/* actual text length, so scan for terminating null character.	*/
			for (i = 0; i < 4; i++) {
				if (text[i]) {
					putchar(text[i]);
				}
				else {
					/* null character found.  no more valid text data.		*/
					length = 0;
					break;
				}
			}

			if (length <= 4) {
				break;
			}
			length -= 4;
		}
	}
	return 1;
}
/* End of File */
